<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
	</head>
	<body>
		<div id="app">
			<post-list></post-list>
		</div>
	
	    <script src="../../vue.js"></script>
		<script>
		    const app = Vue.createApp({})
			// 父组件
			app.component('PostList', {
      			data() {
      				return {
      					posts: [
      						{id: 1, title: '《Servlet/JSP深入详解》怎么样', author: '张三', date: '2019-10-21 20:10:15', vote: 0},
      						{id: 2, title: '《VC++深入详解》观后感', author: '李四', date: '2019-10-10 09:15:11', vote: 0},
      						{id: 3, title: '《Java无难事》怎么样', author: '王五', date: '2020-11-11 15:22:03', vote: 0}
      					]
      				}
      			},
      			methods: {
      				// 自定义事件vote的事件处理器方法
      				handleVote(id){
      					this.posts.map(item => {
      						item.id === id ? {...item, voite: ++item.vote} : item;
      					})
      				}
      			},
      			render(){
      				let postNodes = [];
      				// this.posts.map取代v-for指令，循环遍历posts，
      				// 构造子组件的虚拟节点
      				this.posts.map(post => {
						console.log("Vue.resolveComponent('PostListItem') = ",Vue.resolveComponent('PostListItem'));
      					let node = Vue.h(Vue.resolveComponent('PostListItem'), {
    								post: post,
    								onVote: () => this.handleVote(post.id)
    							});
					    console.log("node = ",node)
      					postNodes.push(node);
      				})
      				return Vue.h('div', [
      						Vue.h('ul',	[
      								postNodes
      							]
      						)
      					]
      				);
      			},
      		});
  		
  		    // 子组件
			app.component('PostListItem', {
				props: {
					post: {
					    type: Object,
					    required: true
					}
				},
				render(){
					return Vue.h('li', [
							Vue.h('p', [
									Vue.h('span',
										// 这是<span>元素的内容
										'标题：'+ this.post.title + ' | 发帖人：' + this.post.author + ' | 发帖时间：' + this.post.date + ' | 点赞数：' + this.post.vote
									),
									Vue.h('button', {
									    onClick: () => this.$emit('vote')
		
										},'赞')
								]
							)
						]
					);
				}
  		    });
  		
			app.mount('#app')
		</script>
	</body>
</html>